# import pandas as pd
# # 读取CSV文件并保留空值
# df = pd.read_csv("../data/semantic_evolution/topic_word_evolution/combined_word_evolution.csv", keep_default_na=False)
# print(df)

import csv


def process_csv(filename1,filename2):
    triples = []
    column_headers = []
    row_names = []
    values = []  # 用于存储所有第三维度的值，方便计算最大最小值

    with open(filename1, 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)

        # 读取第一行获取列名
        headers = next(reader)
        column_headers = headers[1:]  # 除去第一个空值

        # 读取剩余行
        for row in reader:
            if not row:
                continue  # 跳过空行
            row_name = row[0]
            row_names.append(row_name)  # 添加到行名列表

            # 处理当前行的数据
            row_values = row[1:]
            for col_header, value in zip(column_headers, row_values):
                try:
                    num_value = int(value)
                except ValueError:
                    num_value = value

                triples.append([row_name, col_header, num_value])
                if isinstance(num_value, (int, float)):  # 仅统计数值型数据
                    values.append(num_value)


    # 计算最大值和最小值（如果存在数值数据）
    max_value = max(values) if values else None
    min_value = min(values) if values else None

    x_data=[]
    y_data=[]
    with open(filename2, 'r', newline='') as csvfile:
        reader = csv.reader(csvfile)
        # 读取第一行获取列名
        headers = next(reader)
        # 读取剩余行
        for row in reader:
            if not row:
                continue  # 跳过空行
            x_data.append(row[0])
            y_data.append(row[1])
    print(len(x_data),len(list(set(x_data))))
    print(column_headers)
    print(x_data==column_headers,"关键值，如果不是true则出现顺序问题")

    return {
        "headMapData": {
            "data": triples,
            "column_headers": column_headers,
            "row_names": row_names,
            "max_value": max_value,
            "min_value": min_value
        },
        "barData":{
            "x_data":x_data,
            "y_data":y_data
        }
    }


# 示例使用
result = process_csv('../data/semantic_evolution/topic_word_evolution/combined_word_evolution.csv',"../data/semantic_evolution/topic_word_evolution/combined_word_evolution_bar.csv")
print(result["barData"])
